import { ElMessage } from 'element-plus';
export const activityData = [
  {
    type: 'promo',
    title: '营销',
    children: {
      full_coupon: {
        title: '优惠券',
        subtitle: '向客户发送店铺优惠券',
      },
      full_reduce: {
        title: '满额立减',
        subtitle: '满足活动条件享受立减优惠',
      },
      full_discount: {
        title: '满额折扣',
        subtitle: '满足活动条件享受折扣优惠',
      },
      full_gift: {
        title: '满赠',
        subtitle: '吸引客流，刺激消费',
      },
      free_shipping: {
        title: '满额包邮',
        subtitle: '满足活动条件享受包邮优惠',
      },
    },
  },
  {
    type: 'activity',
    title: '活动',
    children: {
      groupon: {
        title: '普通拼团',
        subtitle: '多人拼团享优惠',
      },
      groupon_ladder: {
        title: '阶梯拼团',
        subtitle: '人数越多价格越优惠',
      },
      seckill: {
        title: '秒杀',
        subtitle: '限时特卖引流涨粉',
      },
    },
  },
  {
    type: 'app',
    title: '应用',
    children: {
      score_shop: {
        title: '积分商城',
        subtitle: '引导客户积分消费有效促活',
      },
      signin: {
        title: '签到',
        subtitle: '签到享好礼，客户更活跃',
      },
      wechat_mplive: {
        title: '微信小程序直播',
        subtitle: '一键同步直播间,管理直播间',
      },
    },
  },
];

export function getActivityForm(atype) {
  let form = {
    model: {
      title: '',
      type: atype,
      dateTime: [],
      start_time: '',
      end_time: '',
      richtext_id: '',
      richtext_title: '',
    },
    rules: {
      title: [{ required: true, message: '请输入活动名称', trigger: 'blur' }],
      dateTime: [{ required: true, message: '请选择活动时间', trigger: 'blur' }],
      start_time: [{ required: true, message: '请选择活动开始时间', trigger: 'blur' }],
      end_time: [{ required: true, message: '请选择活动结束时间', trigger: 'blur' }],
      prehead_time: [{ required: true, message: '请选择预热时间', trigger: 'blur' }],
      goods_list: [{ required: true, message: '请选择商品', trigger: 'blur' }],
    },
  };
  let tempForm = {
    // 满减
    full_reduce: {
      model: {
        rules: {
          type: 'money',
          discounts: [
            {
              full: '',
              discounts: '',
            },
          ],
        },
        goods_ids: null,
        goods_list: [],
      },
      rules: {
        discounts: {
          full: [{ required: true, message: '请输入', trigger: 'blur' }],
          discount: [{ required: true, message: '请输入', trigger: 'blur' }],
        },
      },
    },
    full_discount: {
      model: {
        rules: {
          type: 'money',
          discounts: [
            {
              full: '',
              discounts: '',
            },
          ],
        },
        goods_ids: null,
        goods_list: [],
      },
      rules: {
        discounts: {
          full: [
            {
              required: true,
              message: '请输入',
              trigger: 'blur',
            },
          ],
          discount: [
            {
              required: true,
              message: '请输入',
              trigger: 'blur',
            },
          ],
        },
      },
    },
    full_gift: {
      model: {
        rules: {
          limit_num: 0, // 参与次数 0=不限制
          type: 'money', // 优惠类型 money=满足金额 num=满足件数
          event: 'paid', // 赠送时机 paid=支付完成 confirm=确认收货 finish=交易完成
          discounts: [
            {
              full: '',
              gift_num: '', // 礼品份数
              types: [], // 赠送类型 coupon_ids=优惠券 score=积分 money=余额
              coupon_list: [], // 暂存数据
              coupon_ids: '',
              total: '',
              score: '',
              money: '',
              // goods_ids:"",
            },
          ],
        },
        goods_ids: null,
        goods_list: [],
      },
      rules: {
        discounts: {
          full: [
            {
              required: true,
              message: '请输入',
              trigger: 'blur',
            },
          ],
          gift_num: [
            {
              required: true,
              message: '请输入',
              trigger: 'blur',
            },
          ],
          types: [
            {
              required: true,
              message: '请选择赠送类型',
              trigger: 'blur',
            },
          ],
        },
      },
    },
    free_shipping: {
      model: {
        rules: {
          type: 'money',
          full_num: '',
          province_except: '', // 区
          city_except: '', // 市
          district_except: '', // 街道
          district_text: {}, // label数据
        },
        goods_ids: null,
        goods_list: [],
      },
      rules: {
        full_num: { required: true, message: '请输入', trigger: 'blur' },
      },
    },
    groupon: {
      model: {
        prehead_time: '',
        rules: {
          is_commission: 0, // 是否参与分销
          is_free_shipping: 0, // 是否包邮
          sales_show_type: 'real', // real=真实活动销量|goods=商品总销量（包含虚拟销量）
          team_num: 2, // 成团人数，最少两人
          is_alone: 0, // 是否允许单独购买
          is_fictitious: 0, // 是否允许虚拟成团
          fictitious_num: 0, // 最多虚拟人数 0:不允许虚拟 '' 不限制
          fictitious_time: 0, // 开团多长时间自动虚拟成团
          is_team_card: 0, // 参团卡显示
          is_leader_discount: 0, // 团长优惠
          valid_time: 24, // 组团有效时间, 0：一直有效
          limit_num: 0, // 每人限购数量 0:不限购
          refund_type: 'back', // 退款方式 back=原路退回|money=退回到余额
          order_auto_close: 5, // 订单自动关闭时间，如果为 0 将使用系统级订单自动关闭时间
        },
        goods_ids: null,
        goods_list: [],
      },
      rules: {
        valid_time: [{ required: true, message: '请输入拼团解散时间', trigger: 'blur' }],
        team_num: [{ required: true, message: '请输入成团人数', trigger: 'blur' }],
        fictitious_num: [{ required: true, message: '请输入最多虚拟人数', trigger: 'blur' }],
        fictitious_time: [{ required: true, message: '请输入虚拟成团时间', trigger: 'blur' }],
        order_auto_close: [
          { required: true, message: '请输入订单支付时间', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              if (Number(value) <= 0) {
                callback(new Error('值必须大于0'));
              } else {
                callback();
              }
            },
            trigger: 'blur',
          },
        ],
      },
    },
    groupon_ladder: {
      model: {
        prehead_time: '',
        rules: {
          is_commission: 0, // 是否参与分销
          is_free_shipping: 0, // 是否包邮
          sales_show_type: 'real', // real=真实活动销量|goods=商品总销量（包含虚拟销量）
          ladders: {
            ladder_one: 2,
            ladder_two: 3,
          }, // {ladder_one:2,ladder_two:2,ladder_three:2}
          is_alone: 0, // 是否允许单独购买
          is_fictitious: 0, // 是否允许虚拟成团
          fictitious_num: 0, // 最多虚拟人数 0:不允许虚拟 '' 不限制
          fictitious_time: 0, // 开团多长时间自动虚拟成团
          is_team_card: 0, // 参团卡显示
          is_leader_discount: 0, // 团长优惠
          valid_time: 24, // 组团有效时间, 0：一直有效
          limit_num: 0, // 每人限购数量 0:不限购
          refund_type: 'back', // 退款方式 back=原路退回|money=退回到余额
          order_auto_close: 5, // 订单自动关闭时间，如果为 0 将使用系统级订单自动关闭时间
        },
        goods_ids: null,
        goods_list: [],
      },
      rules: {
        valid_time: [{ required: true, message: '请输入拼团解散时间', trigger: 'blur' }],
        ladder_one: [{ required: true, message: '最少两人', trigger: 'blur' }],
        ladder_two: [{ required: true, message: '最少两人', trigger: 'blur' }],
        ladder_three: [{ required: true, message: '最少两人', trigger: 'blur' }],
        fictitious_num: [{ required: true, message: '请输入最多虚拟人数', trigger: 'blur' }],
        fictitious_time: [{ required: true, message: '请输入虚拟成团时间', trigger: 'blur' }],
        order_auto_close: [
          { required: true, message: '请输入订单支付时间', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              if (Number(value) <= 0) {
                callback(new Error('值必须大于0'));
              } else {
                callback();
              }
            },
            trigger: 'blur',
          },
        ],
      },
    },
    seckill: {
      model: {
        prehead_time: '',
        rules: {
          is_commission: 0, // 是否参与分销
          is_free_shipping: 0, // 是否包邮
          sales_show_type: 'real', // real=真实活动销量|goods=商品总销量（包含虚拟销量）
          limit_num: 0, // 每人限购数量 0:不限购
          order_auto_close: 5, // 订单自动关闭时间，如果为 0 将使用系统级订单自动关闭时间is_commission: 0,  // 是否参与分销
        },
        goods_ids: null,
        goods_list: [],
      },
      rules: {
        order_auto_close: [
          { required: true, message: '请输入订单支付时间', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              if (Number(value) <= 0) {
                callback(new Error('值必须大于0'));
              } else {
                callback();
              }
            },
            trigger: 'blur',
          },
        ],
      },
    },
    signin: {
      model: {
        rules: {
          everyday: 0, // 每日签到固定积分
          is_inc: 0, // 是否递增签到
          inc_num: 0, // 递增奖励
          until_day: 0, // 递增持续天数
          discounts: [], // 连续签到奖励 {full:5, value:10}
          is_replenish: 0, // 是否开启补签
          replenish_days: 1, // 可补签天数 最小1
          replenish_limit: 0, // 补签事件限制，0 不限制
          replenish_num: 1, // 补签所消耗积分
        },
      },
      rules: {
        rules: {
          everyday: [{ required: true, message: '请输入日签奖励', trigger: 'blur' }],
          inc_num: [{ required: true, message: '请输入', trigger: 'blur' }],
          until_day: [{ required: true, message: '请输入', trigger: 'blur' }],
          discounts: {
            full: [{ required: true, message: '请输入', trigger: 'blur' }],
            value: [{ required: true, message: '请输入', trigger: 'blur' }],
          },
          replenish_days: [{ required: true, message: '请输入最多虚拟人数', trigger: 'blur' }],
          replenish_limit: [{ required: true, message: '请输入虚拟成团时间', trigger: 'blur' }],
          replenish_num: [{ required: true, message: '请输入虚拟成团时间', trigger: 'blur' }],
        },
      },
    },
  };
  form.model = {
    ...form.model,
    ...tempForm[atype].model,
  };
  form.rules = { ...form.rules, ...tempForm[atype].rules };
  return form;
}

export function handleForm(submitForm) {
  if (submitForm.type != 'signin') {
    //   处理商品
    let goodsIds = [];
    submitForm.goods_list.forEach((g) => {
      goodsIds.push(g.id);
    });
    submitForm.goods_ids = goodsIds.join(',');
  }

  // 满减
  if (submitForm.type == 'full_reduce') {
    if (submitForm.rules.type == 'money') {
      let flag = true;
      submitForm.rules.discounts.forEach((d) => {
        if (Number(d.full) < Number(d.discount)) {
          flag = false;
        }
      });
      if (!flag) {
        ElMessage({
          message: '请输入正确的规则',
          type: 'warning',
        });
        return;
      }
    }
  }

  // 满赠
  if (submitForm.type == 'full_gift') {
    // 优惠券
    submitForm.rules.discounts.forEach((d) => {
      let couponIds = [];
      let total = 0;
      d.coupon_list.forEach((c) => {
        couponIds.push(c.id);
        total += Number(c.amount);
      });
      d.coupon_ids = couponIds.join(',');
      d.total = total;
      delete d.coupon_list;
    });
  }

  // 阶梯拼团
  if (submitForm.type == 'groupon_ladder') {
    if (
      !(
        (!submitForm.rules.ladders.hasOwnProperty('ladder_three') &&
          Number(submitForm.rules.ladders.ladder_one) <
            Number(submitForm.rules.ladders.ladder_two)) ||
        (submitForm.rules.ladders.hasOwnProperty('ladder_three') &&
          Number(submitForm.rules.ladders.ladder_one) <
            Number(submitForm.rules.ladders.ladder_two) &&
          Number(submitForm.rules.ladders.ladder_two) <
            Number(submitForm.rules.ladders.ladder_three))
      )
    ) {
      ElMessage({
        message: '请输入成团人数(阶梯人数依次增加)',
        type: 'warning',
      });
      return;
    }

    let flag = false;
    submitForm.goods_list.forEach((goods) => {
      if (goods.activity_sku_prices) {
        goods.activity_sku_prices.forEach((sku) => {
          if (sku.status == 'up' && submitForm.rules.ladders.hasOwnProperty('ladder_three')) {
            if (!(sku.hasOwnProperty('ladder_three') && sku.hasOwnProperty('ladder_three'))) {
              flag = true;
            }
          }
        });
      }
    });
    if (flag) {
      ElMessage({
        message: '请完善商品规格信息',
        type: 'warning',
      });
      return;
    }
  }

  submitForm.start_time = submitForm.dateTime[0];
  submitForm.end_time = submitForm.dateTime[1];
  delete submitForm.dateTime;

  return submitForm;
}
